#include "public.hpp"

class TcpClient
{

private:
    string _ip;
    uint16_t _port;
    int _sockfd;

public:
    TcpClient() {}

    TcpClient(string ip, uint16_t port) : _ip(ip), _port(port)
    {
    }

    void Init_Start()
    {
        _sockfd = socket(AF_INET, SOCK_STREAM, 0); // Tcp
        if (_sockfd < 0)
        {
            Log(Fatal, "socket create error");
            exit(SOCK_CREATE_ERROR);
        }

        struct sockaddr_in server;
        // 构建目标主机的socket信息
        memset(&server, 0, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons(_port);
        server.sin_addr.s_addr = inet_addr(_ip.c_str());

        int n = connect(_sockfd, (struct sockaddr *)&server, sizeof(server));
        if (n < 0)
        {
            std::cerr << "connect error" << std::endl;
            exit(3);
        }

        while (true)
        {
            std::cout << "Please Enter# ";
            std::string outstring;
            std::getline(std::cin, outstring);

            ssize_t s = send(_sockfd, outstring.c_str(), outstring.size(), 0); // write
            if (s > 0)
            {
                char inbuffer[1024];
                ssize_t m = recv(_sockfd, inbuffer, sizeof(inbuffer) - 1, 0);
                if (m > 0)
                {
                    inbuffer[m] = 0;
                    std::cout << inbuffer << std::endl;
                }
                else
                {
                    break;
                }
            }
            else
            {
                break;
            }
        }

        ::close(_sockfd);

        // void GetInfo()
        // {
        //     char buffer[1024];
        //     bzero(buffer, sizeof(buffer));

        //     struct sockaddr_in server;
        //     bzero(&server, sizeof(server));

        //     while (true)
        //     {
        //         ssize_t realnum = recv(_sockfd, (struct sockaddr *)&server, sizeof(server), 0);
        //         if (realnum > 0)
        //         {
        //             Analyze ret(server);
        //             // fprintf(stderr, "[client message from: %s]: %s\n", ret.getip().c_str(), buffer);
        //             cout << "[client message from: " << ret.getip() << "]: " << buffer << endl;
        //         }
        //     }
        // }

        // void SendInfo()
        // {
        //     struct sockaddr_in goal;
        //     bzero(&goal, 0);
        //     goal.sin_family = AF_INET;
        //     goal.sin_addr.s_addr = inet_addr(_ip.c_str());
        //     goal.sin_port = htons(_port); // port主机序列转网络序列

        //     string str;
        //     while (true)
        //     {
        //         cout << "Please Enter# ";
        //         getline(cin, str);

        //         // 将消息发送给服务端
        //         ssize_t val = send(_sockfd, (struct sockaddr *)&goal, sizeof(goal), 0);
        //         if (val < 0)
        //         {
        //             Log(Error, "send clientinfo error");
        //             perror("sendto:");
        //             continue;
        //         }
        //     }
        // }

        // void Start()
        // {

        //     struct sockaddr_in goal;
        //     bzero(&goal, 0);
        //     goal.sin_family = AF_INET;
        //     goal.sin_addr.s_addr = inet_addr(_ip.c_str());
        //     goal.sin_port = htons(_port); // port主机序列转网络序列

        //     int n = connect(_sockfd, (struct sockaddr*)&goal, sizeof(goal));
        //     if(n < 0)
        //     {
        //         cout << "connect error" << endl;
        //         exit(1);
        //     }
        //     cout << "connect success" << endl;

        //     ThreadPool<function<void()>> *Pool = ThreadPool<function<void()>>::GetPool();

        //     Pool->ThreadInit();
        //     Pool->Start();

        //     function<void()> func1 = bind(&TcpClient::GetInfo, this);
        //     Pool->Push(func1);

        //     function<void()> func2 = bind(&TcpClient::SendInfo, this);
        //     Pool->Push(func2);

        //     Pool->Wait();
        // }
    }
};